TREES

Reduced child deaths

The Treemap

Photo by Avel Chuklanov on Unsplash

Photo by Avel Chuklanov on Unsplash

History will judge us by the difference we make in the everyday lives of children…
— Nelson Mandela

Every single country has seen a reduction in child deaths (since 1950). Under five mortality rate (% of children that die before they are 5 years old)


Ingest the data

occupations, industries, and number of people in the top 1 percent

# Load data
df = read.csv("./archetypes/reduced-child-deaths/reduced-child-deaths.csv", header = TRUE, stringsAsFactors = FALSE)  # read text file 
df

Wrangle the data

parse numbers and enrich with region

df_wrangle <- df
df_wrangle$X..change.1950.2015 <- as.numeric(sub("%", "", df_wrangle$X..change.1950.2015))
df_wrangle$X..change.1950.2015 <- as.numeric(sub("-", "", df_wrangle$X..change.1950.2015))
df_wrangle$un_region <- countrycode(df_wrangle$under_five_mortality_rate, origin='country.name', destination='un.region.name')
colnames(df_wrangle) <- c("COUNTRY", "MEASURE", "REGION")

df_wrangle <- filter(df_wrangle, nchar(REGION) > 0)
df_wrangle

Analyze the data

Create a quartile column to use for color mapping

The within function is used for calculating new columns. The quantile function calculates the quartiles where 0:4/4 evaluates to c(0, 0.25, 0.50, 0.75, 1). Finally the cut function splits the data into the calculated quartiles. Casting the result to integers returns groups labeled as 1,2,3,4. The column is converted to a factor for use with a discrete color scale.

df_analysis <- within(df_wrangle, quartile <- factor(as.integer(cut(MEASURE, quantile(MEASURE, probs=0:4/4), include.lowest=TRUE))))
df_analysis

The Layout

squarified with size and fill

# Layouts
# squarified" (the default), "scol", "srow" or "fixed"
# Simple
v1 <- ggplot(df_analysis, aes(area = MEASURE, fill = quartile)) +
  geom_treemap(color = "#ffffff", layout = 'fixed') +
  scale_fill_brewer(palette = "PiYG") +
  theme_minimal() +
  theme(legend.position = "bottom")

girafe(ggobj = v1, width_svg = 1280/72, height_svg = 720/72,
       options = list(opts_sizing(rescale = TRUE, width = 1.0))
)

with labels

# Labeled
v2 <- ggplot(df_analysis, aes(area = MEASURE, fill = quartile, label = COUNTRY)) +
  geom_treemap(color = "#ffffff", layout = 'fixed') +
  geom_treemap_text(fontface = "italic", colour = "white", place = "centre", grow = TRUE, layout = 'fixed') +
  scale_fill_brewer(palette = "PiYG") +
  theme_minimal() +
  theme(legend.position = "bottom")

girafe(ggobj = v2, width_svg = 1280/72, height_svg = 720/72,
       options = list(opts_sizing(rescale = TRUE, width = 1.0))
)

with labels and color palette

# Sub-grouping
v3 <- ggplot(df_analysis, aes(area = MEASURE, fill = quartile, label = COUNTRY, subgroup = REGION)) +
  geom_treemap(color = "#ffffff", layout = 'fixed') +
  geom_treemap_subgroup_border(colour = "white", layout = 'fixed') +
  geom_treemap_subgroup_text(place = "centre", grow = T, alpha = 0.5, colour =
                               "black", fontface = "italic", min.size = 0, layout = 'fixed') +
  geom_treemap_text(colour = "white", place = "topleft", reflow = T, layout = 'fixed') +
  scale_fill_brewer(palette = "PiYG") +
  theme_minimal() +
  theme(legend.position = "bottom")

# v3

girafe(ggobj = v3, width_svg = 1280/72, height_svg = 720/72,
       options = list(opts_sizing(rescale = TRUE, width = 1.0))
)

as facets

# Sub-grouping
v4 <- ggplot(df_analysis, 
             aes(area = MEASURE, fill = quartile, label = COUNTRY)) +
  geom_treemap(color = "#ffffff", layout = 'fixed') +
  facet_wrap( ~REGION) +
  scale_fill_brewer(palette = "PiYG") +
  geom_treemap_text(colour = "white", place = "topleft", reflow = T, layout = 'fixed') +
  theme_minimal()+
  theme(legend.position = "bottom")

girafe(ggobj = v4, width_svg = 1280/72, height_svg = 1280/72,
       options = list(opts_sizing(rescale = TRUE, width = 1.0))
)

References

citations for narrative and data sources